<?php

/**
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 */
class UserComponent extends User
{
	public function attributeLabels()
	{
		return array(
			'email' => Yii::t('user','Email'),
			'pass' => Yii::t('user','Password'),
            'confirmpass' => Yii::t('user','Confirm Password'),
			'lastest_login' => Yii::t('user','Lasted Login'),
			'last_name' => Yii::t('user','Last Name'),
			'first_name' => Yii::t('user','First Name'),
			'login_attempts' => Yii::t('user','Login attemps'),
			'active' => Yii::t('user','Active'),
			'group_id' => Yii::t('user','Group'),
			'sex' => Yii::t('user','Sex'),
			'birthday' => Yii::t('user','Birthday'),
			'address' => Yii::t('user','Address'),
			'mobile' => Yii::t('user','Mobile'),
			'active_code' => Yii::t('user','Active Code'),
			'note' => Yii::t('user','Note'),
		);
	}
    
    
    
    /**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
            'group' => array(self::BELONGS_TO, 'GroupComponent', 'group_id')
		);
	}
    
    public function scopes() {
        return array('odearBy' => array('order' => 'name'),
               'alias'=>'u',
               'select' => array('select' => 'u.*')
            );
    }
    
    function isDeactivated($email)
	{
		$bln_ok = false;

		if ($email)
		{
			$user = User::model()->findByAttributes(array('email'=>$email));
            
			if ($user)
			{
                if (!$user->active)
				{
					$bln_ok = true;
				}
			}
		}

		return $bln_ok;
	}
    
    function connecter_utilisateur($login, $pwd) 
    {
        //         $this->password = hash_hmac('sha256',$this->newPassword,'asdof87ygh',true);

        $rows = Yii::app()->db->createCommand()
              ->select('user.*, user_group.name group_name')
              ->from('user')
              ->join('user_group', 'user_group.id = user.group_id')
              ->where('email=:email and pass=:pass and disabled =0', array(':email'=>mysql_escape_string($login),':pass'=>  sha1($pwd)))
              ->queryRow();
        return $rows;
    }
    
    function updateUser($id) {
        $user = User::model()->findByPk($id);
        $user->lastest_login = date("Y-m-d H:i:s");
		$user->update();
	}
    
    function increaseLoginAttempts($email)
	{
		if ($email)
		{
			$user = User::model()->findByAttributes(array('email'=>$email));
			if ($user)
			{
				$user->login_attempts += 1;

				if ($user->login_attempts >= ALLOWED_LOGIN_ATTEMPTS)
				{
					$user->active = 0;
				}

				$user->update();
			}
		}
	}
    
    function resetLoginAttempts($email)
	{
		if ($email)
		{
			$user = User::model()->findByAttributes(array('email'=>$email));
			if ($user)
			{
				if (!$user->active)
				{
					$user->login_attempts = 0;
					$user->update();
				}
			}
		}
	}
    
    function isExistByEmail($email) {
        return (User::model()->findByAttributes(array('email'=>$email)))? '1':'0';
    }
    
    public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.
//die('aaaa');
		$criteria=new CDbCriteria;

		//$criteria->compare('id',$this->id);
		$criteria->compare('email',$this->email,true);
		//$criteria->compare('pass',$this->pass,true);
		$criteria->compare('lastest_login',$this->lastest_login,true);
		$criteria->compare('last_name',$this->last_name,true);
		$criteria->compare('first_name',$this->first_name,true);
		//$criteria->compare('login_attempts',$this->login_attempts);
		$criteria->compare('active',$this->active);
		//$criteria->compare('group_id',$this->group_id);
        $criteria->compare('group.name',$this->group,true);  
        $criteria->with='group'; 
		$criteria->compare('sex',$this->sex);
		$criteria->compare('birthday',$this->birthday,true);
		$criteria->compare('address',$this->address,true);
		$criteria->compare('mobile',$this->mobile,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
    
}